"""
@author: jiangpj
@email: pj.jiang@hzgosun.com
@time: 2019/5/10 11:32

MAC数据清洗的增删改查自动化测试用例编写

"""
import unittest
from time import sleep
from selenium.webdriver.common.keys import Keys

from common.settings import TAG
from src.common.decorators import log4gs, mkdir_classname
from src.common.functions import save_screenshot
from src.common.vue.select import DropDownSelect
from src.pages.macManagePage import MacManagePage


@log4gs
@mkdir_classname(TAG)
class MacManageTest(unittest.TestCase):
    """MAC数据清洗功能测试"""
    @classmethod
    def setUpClass(cls):
        cls.log.info('=' * 30 + "开始MAC数据清洗功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page = MacManagePage('/PopcManage/MacManage')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('=' * 30 + "结束MAC数据清洗功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page.driver.quit()

    def setUp(self):

        self.log.info('>>>>>开始执行用例：{} {}'.format(self.id(), self.shortDescription()))

    def tearDown(self):
        sleep(3)
        self.log.info('<<<<<结束执行用例：{} {}'.format(self.id(), self.shortDescription()))

    # @unittest.skip
    def test_1_page(self):
        """MAC数据清洗翻页"""
        sleep(1)
        self.log.info("共有：{}页数据".format(self.page.turnPage.total_page))
        self.log.info("当前处于第：{}页数据".format(self.page.turnPage.current_page))
        self.log.info("共有：{}条数据".format(self.page.turnPage.total_num))
        sleep(2)
        self.assertNotEqual(self.page.turnPage.total_page, 1, msg="页数过少，无法进行翻页测试")
        try:
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')
            sleep(2)
            try:
                self.page.turnPage.next_page.click()
                self.assertEqual(self.page.turnPage.current_page, '3')
                sleep(2)
                self.page.turnPage.prev_page.click()
                self.assertEqual(self.page.turnPage.current_page, '2')
                sleep(2)
                self.page.turnPage.input.clear()
                self.page.turnPage.input.send_keys(3)
                self.page.turnPage.input.send_keys(Keys.ENTER)
                self.assertEqual(self.page.turnPage.current_page, '3')
            except:
                self.log.error("当前模块最大为2页，无法进行翻页测试", exc_info=True)
                save_screenshot(self,'翻页功能测试失败')
        except Exception:
            self.log.error("当前模块最大为1页，无法进行翻页测试", exc_info=True)
            save_screenshot(self, '翻页功能测试失败')

    @unittest.skip
    def test_2_1_time(self):
        """开始时间检索"""
        time = '2019-03-01 00:00:00'
        mac = '3F:58:47:3C:15:B0'
        self.page.searchTools.start_time.send_keys(time)
        self.page.searchTools.search_btn.click()
        sleep(1)
        result = self.page.table.get_row(mac)
        sleep(1)
        try:
            self.page.searchTools.start_time.clear()
            self.assertTrue(result, "通过设备:{}，mac查询到了{}".format(time, mac))
            self.log.info("用例{}设备:{}检索执行成功".format(self.id(), time))

        except Exception:
            self.log.error("用例{}设备：{}检索执行失败,".format(self.id(), time), exc_info=True)

    # @unittest.skip
    def test_2_2_facility(self):
        """设备功能检索"""
        she = "金海电围2"
        mac = '30:6C:45:59:05:FC'
        DropDownSelect(self.page.searchTools.facility, she)
        self.page.searchTools.search_btn.click()
        sleep(1)
        result = self.page.table.get_row(mac)
        sleep(1)
        self.page.searchTools.facility.clear()
        try:
            self.assertTrue(result, "通过设备:{}，mac查询到了{}".format(she, mac))
            self.log.info("用例{}设备:{}检索执行成功".format(self.id(), she))

        except Exception:
            self.log.error("用例{}设备：{}检索执行失败,".format(self.id(), she), exc_info=True)


    # @unittest.skip
    def test_2_3_search(self):
        """已绑定检索"""
        bang = "已绑定"
        mac = '30:6C:45:59:05:FC'
        DropDownSelect(self.page.searchTools.bound, bang)
        self.page.searchTools.search_btn.click()
        sleep(1)
        result = self.page.table.get_row(mac)
        self.page.searchTools.bound.clear()
        sleep(1)
        try:
            self.assertTrue(result, "通过{}，mac查询到了{}".format(bang, mac))
            self.log.info("用例{}绑定{}执行成功".format(self.id(), bang))

        except Exception:
            self.log.error("用例{}绑定{}执行失败,".format(self.id(), bang), exc_info=True)

    @unittest.skip
    def test_2_4_time(self):
        """组合时间绑定"""
        province = "河南省郑州市二七区金海小区"
        plot = "金海小区"
        self.page.searchTools.start_time.send_keys('2019-05-01 00:00:00')
        sleep(2)
        self.page.searchTools.end_time.send_keys('2019-07-23 00:00:00')
        sleep(1)
        self.page.searchTools.reset_btn.click()
        sleep(1)
        DropDownSelect(self.page.addwin.plot, province)
        sleep(1)
        self.page.addwin.cancel.click()
        sleep(1)
        result = self.page.table.get_row(plot)
        self.page.searchTools.start_time.clear()
        sleep(1)
        self.page.searchTools.end_time.clear()
        sleep(1)
        try:
            self.assertTrue(result, plot)
            self.log.info("用例{}组合绑定{}执行成功".format(self.id(), province))
        except:
            self.log.error("用例{}组合绑定{}执行失败,".format(self.id(), province), exc_info=True)

    # @unittest.skip
    def test_3_1_bulk_binds(self):
        """批量绑定"""
        province = "二七区金海小区"
        plot = "金海小区"
        self.page.table.checkbox.click()
        sleep(1)
        self.page.table.re_btn.click()
        sleep(1)
        self.page.addwin.plot_input.send_keys(plot)
        sleep(1)
        DropDownSelect(self.page.addwin.plot, province)
        self.page.addwin.confirm.click()
        sleep(1)
        result = self.page.table.get_row(plot)
        sleep(1)
        try:
            self.assertTrue(result, plot)
            self.log.info("用例{}批量绑定{}执行成功".format(self.id(), province))
        except Exception:
            self.log.error("用例{}批量绑定{}执行失败,".format(self.id(), province), exc_info=True)

    # @unittest.skip
    def test_3_2_checkbox(self):
        """点击查询结果里面某行的checkbox"""
        mac = '30:6C:45:59:05:FC'
        row = self.page.table.get_row(mac)
        try:
            row_checkbox = row.find_element_by_class_name('ivu-checkbox-input')
            row_checkbox.click()
        except Exception:
            self.log.error("没有找到checkbox")


if __name__ == '__main__':
    unittest.main()
